package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.InformationDao;
import ysq.xyg.model.Information;
import ysq.xyg.service.InformationService;

import javax.annotation.Resource;
import java.util.List;

@Service("informationService")
public class InformationServiceImpl extends BaseServiceImpl<Information> implements InformationService {

    @Resource
    private InformationDao informationDao;
    @Autowired
    public void setInformationDao(InformationDao informationDao) {
        this.baseDao = informationDao;
    }

    @Override
    public PageInfo<Information> pageInfo(Information information, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("sort desc");

        List<Information> list = list(information);
        return new PageInfo<Information>(list);
    }
    @Override
    public PageInfo<Information> pageLeft(Information information, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("sort desc");
        List<Information> list = informationDao.listLeft(information);
        return new PageInfo<Information>(list);
    }

    @Override
    public List<Information> list(Information information) {
        // 通用Example查询
        Example example = new Example(Information.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(information.getStatus())) {
            criteria.andEqualTo("status", information.getStatus());
        }
        if (StringUtils.isNotEmpty(information.getShowFrist())) {
            criteria.andEqualTo("showFrist", information.getShowFrist());
        }
        if (StringUtils.isNotEmpty(information.getCatalogId())) {
            criteria.andEqualTo("catalogId", information.getCatalogId());
        }
        if (StringUtils.isNotEmpty(information.getTitle())) {
            criteria.andLike("title", "%" + information.getTitle() + "%");
        }
        List<Information> list = informationDao.selectByExample(example);
        return list;
    }
}
